<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\controller\Backend;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 8; $i++) {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = model('VideoOrder')->where('createtime', '>=', $seventtime + ($i * 86400))
                ->where('createtime', '<', $seventtime + (($i + 1) * 86400))->count();
            $paylist[$day] = model('VideoOrder')->where('type', 2)
                ->where('createtime', '>=', $seventtime + ($i * 86400))
                ->where('createtime', '<', $seventtime + (($i + 1) * 86400))->count();
            $guestlist[$day] = model('Guest')->where('createtime', '>=', $seventtime + ($i * 86400))
                ->where('createtime', '<', $seventtime + (($i + 1) * 86400))->count();
            $viewlist[$day] = model('Videolog')->where('createtime', '>=', $seventtime + ($i * 86400))
                ->where('createtime', '<', $seventtime + (($i + 1) * 86400))->count();
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $this->view->assign([
            'totaluser' => User::count(),
            'totalviews' => model('Videolist')->sum('plays'),
            'totalorder' => model('Paylog')->where('type', 2)->count(),
            'totalorderamount' => model('Paylog')->where('type', 2)->sum('amount'),
            'todayguest' => model('Guest')->where('createtime', '>', \fast\Date::unixtime('day'))->count(),
            'todayuserlogin' => model('User')->where('logintime', '>', \fast\Date::unixtime('day'))->count(),
            'todayusersignup' => model('User')->where('jointime', '>', \fast\Date::unixtime('day'))->count(),
            'todayviews' => model('Videolog')->where('createtime', '>', \fast\Date::unixtime('day'))->count(),
            'todayviewtimes' => intval(model('Videolog')->where('createtime', '>', \fast\Date::unixtime('day'))->sum('vtime') / 60),
            'todayshares' => model('Videoshare')->where('createtime', '>', \fast\Date::unixtime('day'))->count(),
            'todayorder' => model('Paylog')->where('type', 2)->where('createtime', '>', \fast\Date::unixtime('day'))->count(),
            'unsettleorder' => model('Paylog')->where('type', 1)->where('createtime', '>', \fast\Date::unixtime('day'))->count(),
            'sevendnu' => model('User')->where('jointime', '>', \fast\Date::unixtime('day', -7))->count(),
            'sevendau' => model('User')->where('logintime', '>', \fast\Date::unixtime('day', -7))->count(),
            'paylist' => $paylist,
            'guestlist' => $guestlist,
            'viewlist' => $viewlist,
            'createlist' => $createlist,
            'addonversion' => $addonVersion,
            'uploadmode' => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
